package com.gxa.meiye.admin.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gxa.meiye.admin.service.ProjectService;
import com.gxa.meiye.admin.utils.TokenUtil;
import com.gxa.meiye.core.common.JsonResult;
import com.gxa.meiye.core.pojo.entity.Projects;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @date 2022/7/19 10:05
 */
@Api(tags = "项目模块")
@Controller
@RequestMapping("/mlsproject")
public class ProjectController {
    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private ProjectService projectService;

    @ApiOperation("添加项目")
    @ResponseBody
    @PostMapping("/addpro")
    public JsonResult addProject(Projects project){
        Integer addPro = this.projectService.addPro(project);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("操作成功");
        if (addPro == 0){
            jsonResult.setCode("201");
            jsonResult.setMsg("操作失败");
        }
        return jsonResult;
    }

    @ApiOperation("修改项目")
    @ResponseBody
    @PutMapping("/update")
    public JsonResult updateProject(Projects project){
        System.out.println(project);
        boolean b = this.projectService.updatePro(project);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("操作成功");
        if (!b){
            jsonResult.setCode("201");
            jsonResult.setMsg("操作失败");
        }
        return jsonResult;
    }

    @ApiOperation("下架项目")
    @ResponseBody
    @PutMapping("/stop")
    public JsonResult stopProject(@ApiParam(name = "id",value = "项目的id",type = "Integer")Integer id){
        Integer stopPro = this.projectService.stopPro(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("操作成功");
        if (stopPro == 0){
            jsonResult.setCode("201");
            jsonResult.setMsg("操作失败");
        }
        return jsonResult;
    }

    @ApiOperation("查询我的负责项目")
    @ResponseBody
    @GetMapping("/list")
    public JsonResult lisyProject(ServletRequest request) throws JsonProcessingException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String token = httpRequest.getHeader("token");
        Map user = tokenUtil.getUserByToken(token);
        Integer id = (Integer)user.get("id");
        List<Projects> projects = this.projectService.queryPro(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("操作成功");
        jsonResult.setData(projects);
        if (projects.size()==0){
            jsonResult.setCode("201");
            jsonResult.setMsg("请添加项目");
        }
        return jsonResult;
    }


    @ApiOperation("删除项目")
    @ResponseBody
    @DeleteMapping("/delete")
    public JsonResult deleteProject(@ApiParam(name = "id",value = "项目的id",type = "Integer")Integer id){
        boolean b = this.projectService.removeById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("操作成功");
        if (b == false){
            jsonResult.setCode("201");
            jsonResult.setMsg("操作失败");
        }
        return jsonResult;
    }


    @ApiOperation("项目详情")
    @ResponseBody
    @GetMapping("/pro")
    public JsonResult getProject(@ApiParam(name = "id",value = "项目的id",type = "Integer") Integer id){
        Projects query = this.projectService.getOne(new LambdaQueryWrapper<Projects>()
                .eq(Projects::getId, id));

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("操作成功");
        jsonResult.setData(query);
        return jsonResult;
    }



}
